package com.ccp.dev.workflow.model;

import com.ccp.dev.core.basic.model.BaseModel;
import com.ccp.dev.core.util.StringUtil;

import java.util.Arrays;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 * ClassDescribe: 流程模型
 * @author :jiayueqiao
 */
public class ActDefModel extends BaseModel<ActDefModel>{

    private static final Pattern pattern = Pattern.compile("([\\w-]+):([\\w-]+)");

    /**
     * 流程定义规则
     */
    public static final String DEFAULT_SUBJECT_RULE="{流程标题:title}-{发起时间:startTime}-{发起人:startUser}";
    /**
     * 默认测试状态的标签
     */
    public static final String TEST_TAG="测试状态";

    /**
     * 为主流程 main=1
     */
    public final static Short MAIN=1;
    /**
     * 非主流程，即为历史版本   main=0;
     */
    public final static Short NOT_MAIN=0;

    /**
     * 未发布
     */
    public final static Short STATUS_UNDEPLOYED=0;
    /**
     * 可用
     */
    public final static Short STATUS_ENABLED=1;
    /**
     * 禁用
     */
    public final static Short STATUS_DISABLED=2;
    /**
     *禁用流程实例
     */
    public final static Short STATUS_INST_DISABLED=3;

    /**
     *试运行
     */
    public final static Short STATUS_TEST=4;


    /**
     * 挂起
     */
    public final static Short STATUS_SUSPEND=-1;

    /**
     * 允许=1
     */
    public final static Short ALLOW =1;
    /**
     * 不允许=0
     */
    public final static Short NOT_ALLOW =0;

    /**
     * 表名称
     */
    public final static String TABLE_NAME="act_de_model";

    /**
     * 主版本
     */
    public final static int IS_MAIN = 1;
    /**
     * 非主版本
     */
    public final static int IS_NOT_MAIN =0;

    /**
     * 节点跳过设定，全部跳过
     */
    public final static String SKIPSETTING_GLOBAL="global";
    /**
     * 节点跳过设定，相同执行人跳过
     */
    public final static String SKIPSETTING_SAMEUSER="sameUser";
    /**
     * 节点跳过设定，之前审批过后跳过
     */
    public final static String SKIPSETTING_APPROVEUSER="approveUser";




    /**
     * 主键
     */
    private String id;

    /**
     * 流程分类id
     */
    private String typeId;

    /**
     * 模型名称
     */
    private String name;

    /**
     * 模型key
     */
    private String modelKey;

    /**
     * 任务标题生成规则
     */
    private String taskNameRule;

    /**
     * 模型描述
     */
    private String description;

    /**流程状态
     * 0:未发布
     * 1:发布
     * 2.禁用
     * 3.禁用,并禁用实例
     * 4.测试状态
     */
    private Short status;

    /**
     * 模型Json数据
     */
    private String modelEditorJson;

    /**
     * activiti流程发布ID
     */
    private String deployId;

    /**
     * act流程定义Key
     */
    private String procDefKey;

    /**
     * procDefId流程定义ID
     */
    private String procDefId;

    /**
     * 发布新版的备注
     */
    private String modelComment;

    /**
     * 模型版本
     */
    private int version;

    /**
     * 流程分类名称
     */
    private String typeName;

    /**
     * 跳过第一个任务节点, 默认为跳过
     */
    private Short toFirstNode=1;

    /**
     * 流程启动时  是否可以选择下一步的执行人
     */
    private Short showFirstAssignee=0;


    /**
     * 可以选择条件同步路径的节点
     */
    private String canChoicePath;

    /**
     * 可选择路径的节点MAP
     */
    private Map canChoicePathNodeMap;

    /**
     * 流程定义是否调用外部表单
     */
    private Short isUseOutForm=0;

    /**
     * 表单明细url
     */
    private String formDetailUrl;

    /**
     * 是否允许流程结束时抄送
     */
    private Integer allowFinishedCc = 0;

    /**
     * 提交是否需要确认
     */
    private Integer submitConfirm=0;

    /**
     * 是否允许转办
     */
    private Integer allowDivert=1;

    /**
     * 归档时发送消息给发起人 类型
     */
    private String informStart;

    /**
     * 通知类型
     */
    private String informType;

    /**
     * 帮助附件
     */
    private String attachment;

    /**
     * 相同任务节点是否允许跳转。
     * 0：不允许
     * 1：允许
     */
    private Short sameExecutorJump=0;

    /**
     * 是否允许流程参考
     */
    private Integer allowRefer=0;

    /**
     * 允许实例数量
     */
    private Integer instanceAmount=5;

    /**
     * 是否允许我的办结转发
     */
    private Integer allowFinishedDivert=1;

    /**
     * 是否开启打印模版功能
     */
    private	Short	isPrintForm=0;

    /**
     * 直接启动不需要使用表单
     */
    private Integer directstart=0;

    /**
     * 流程分管授权权限对象
     */
    private AuthorizeRight authorizeRight;

    /**
     * 抄送消息类型
     */
    private String ccMessageType;

    /**
     * 测试状态标签
     */
    private String testStatusTag;


    /**
     * 流程是否支持手机访问
     * 0=不支持
     * 1=支持
     */
    private Integer allowMobile = 1;

    /**
     * 是否允许撤销
     * 0:不允许
     * 1:允许
     */
    private Integer allowRevert=1;

    /**
     * 跳过设定
     */
    private String skipSetting="";

    /**
     * 是否主版本
     */
    private int isMain;

    /**
     * 父模型id
     */
    private String parentModelId;

    /**
     * bpmn模型图
     */
    private byte[] thumbnail;

    public String getId() {
        return id;
    }

    public void setId(String id) {
        this.id = id;
    }

    public String getTypeId() {
        return typeId;
    }

    public void setTypeId(String typeId) {
        this.typeId = typeId;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getModelKey() {
        return modelKey;
    }

    public void setModelKey(String modelKey) {
        this.modelKey = modelKey;
    }

    public String getTaskNameRule() {
        return taskNameRule;
    }

    public void setTaskNameRule(String taskNameRule) {
        this.taskNameRule = taskNameRule;
    }

    public String getDescription() {
        return description;
    }

    public void setDescription(String description) {
        this.description = description;
    }

    public Short getStatus() {
        return status;
    }

    public void setStatus(Short status) {
        this.status = status;
    }

    public String getModelEditorJson() {
        return modelEditorJson;
    }

    public void setModelEditorJson(String modelEditorJson) {
        this.modelEditorJson = modelEditorJson;
    }

    public String getDeployId() {
        return deployId;
    }

    public void setDeployId(String deployId) {
        this.deployId = deployId;
    }

    public String getProcDefKey() {
        return procDefKey;
    }

    public void setProcDefKey(String procDefKey) {
        this.procDefKey = procDefKey;
    }

    public String getProcDefId() {
        return procDefId;
    }

    public void setProcDefId(String procDefId) {
        this.procDefId = procDefId;
    }

    public String getModelComment() {
        return modelComment;
    }

    public void setModelComment(String modelComment) {
        this.modelComment = modelComment;
    }

    public int getVersion() {
        return version;
    }

    public void setVersion(int version) {
        this.version = version;
    }

    public String getTypeName() {
        return typeName;
    }

    public void setTypeName(String typeName) {
        this.typeName = typeName;
    }

    public Short getToFirstNode() {
        return toFirstNode;
    }

    public void setToFirstNode(Short toFirstNode) {
        this.toFirstNode = toFirstNode;
    }

    public Short getShowFirstAssignee() {
        return showFirstAssignee;
    }

    public void setShowFirstAssignee(Short showFirstAssignee) {
        this.showFirstAssignee = showFirstAssignee;
    }

    public String getCanChoicePath() {
        return canChoicePath;
    }

    public void setCanChoicePath(String canChoicePath) {
        this.canChoicePath = canChoicePath;
    }

    public Map getCanChoicePathNodeMap() {
        if(canChoicePathNodeMap==null){
            Map map = new HashMap();
            if(StringUtil.isEmpty(this.canChoicePath)){
                canChoicePathNodeMap = map;
                return canChoicePathNodeMap;
            }
            Matcher regexMatcher = pattern.matcher(this.canChoicePath);
            while (regexMatcher.find()) {
                map.put(regexMatcher.group(1), regexMatcher.group(2));
            }
            canChoicePathNodeMap = map;
        }
        return canChoicePathNodeMap;
    }

    public void setCanChoicePathNodeMap(Map canChoicePathNodeMap) {
        this.canChoicePathNodeMap = canChoicePathNodeMap;
    }

    public Short getIsUseOutForm() {
        return isUseOutForm;
    }

    public void setIsUseOutForm(Short isUseOutForm) {
        this.isUseOutForm = isUseOutForm;
    }

    public String getFormDetailUrl() {
        return formDetailUrl;
    }

    public void setFormDetailUrl(String formDetailUrl) {
        this.formDetailUrl = formDetailUrl;
    }

    public Integer getAllowFinishedCc() {
        return allowFinishedCc;
    }

    public void setAllowFinishedCc(Integer allowFinishedCc) {
        this.allowFinishedCc = allowFinishedCc;
    }

    public Integer getSubmitConfirm() {
        return submitConfirm;
    }

    public void setSubmitConfirm(Integer submitConfirm) {
        this.submitConfirm = submitConfirm;
    }

    public Integer getAllowDivert() {
        return allowDivert;
    }

    public void setAllowDivert(Integer allowDivert) {
        this.allowDivert = allowDivert;
    }

    public String getInformStart() {
        return informStart;
    }

    public void setInformStart(String informStart) {
        this.informStart = informStart;
    }

    public String getInformType() {
        return informType;
    }

    public void setInformType(String informType) {
        this.informType = informType;
    }

    public String getAttachment() {
        return attachment;
    }

    public void setAttachment(String attachment) {
        this.attachment = attachment;
    }

    public Short getSameExecutorJump() {
        return sameExecutorJump;
    }

    public void setSameExecutorJump(Short sameExecutorJump) {
        this.sameExecutorJump = sameExecutorJump;
    }

    public Integer getAllowRefer() {
        return allowRefer;
    }

    public void setAllowRefer(Integer allowRefer) {
        this.allowRefer = allowRefer;
    }

    public Integer getInstanceAmount() {
        return instanceAmount;
    }

    public void setInstanceAmount(Integer instanceAmount) {
        this.instanceAmount = instanceAmount;
    }

    public Integer getAllowFinishedDivert() {
        return allowFinishedDivert;
    }

    public void setAllowFinishedDivert(Integer allowFinishedDivert) {
        this.allowFinishedDivert = allowFinishedDivert;
    }

    public Short getIsPrintForm() {
        return isPrintForm;
    }

    public void setIsPrintForm(Short isPrintForm) {
        this.isPrintForm = isPrintForm;
    }

    public Integer getDirectstart() {
        return directstart;
    }

    public void setDirectstart(Integer directstart) {
        this.directstart = directstart;
    }

    public AuthorizeRight getAuthorizeRight() {
        return authorizeRight;
    }

    public void setAuthorizeRight(AuthorizeRight authorizeRight) {
        this.authorizeRight = authorizeRight;
    }

    public String getCcMessageType() {
        return ccMessageType;
    }

    public void setCcMessageType(String ccMessageType) {
        this.ccMessageType = ccMessageType;
    }

    public String getTestStatusTag() {
        return testStatusTag;
    }

    public void setTestStatusTag(String testStatusTag) {
        this.testStatusTag = testStatusTag;
    }

    public Integer getAllowMobile() {
        return allowMobile;
    }

    public void setAllowMobile(Integer allowMobile) {
        this.allowMobile = allowMobile;
    }

    public Integer getAllowRevert() {
        return allowRevert;
    }

    public void setAllowRevert(Integer allowRevert) {
        this.allowRevert = allowRevert;
    }

    public String getSkipSetting() {
        return skipSetting;
    }

    public void setSkipSetting(String skipSetting) {
        this.skipSetting = skipSetting;
    }

    public byte[] getThumbnail() {
        return thumbnail;
    }

    public void setThumbnail(byte[] thumbnail) {
        this.thumbnail = thumbnail;
    }

    public int getIsMain() {
        return isMain;
    }

    public void setIsMain(int isMain) {
        this.isMain = isMain;
    }

    public String getParentModelId() {
        return parentModelId;
    }

    public void setParentModelId(String parentModelId) {
        this.parentModelId = parentModelId;
    }

    /**
     * 更新可以选择路径的节点信息
     */
    public void updateCanChoicePath(){
        if(this.canChoicePathNodeMap!=null){
            StringBuffer sb=new StringBuffer();
            Iterator iter = this.canChoicePathNodeMap.entrySet().iterator();
            while (iter.hasNext()) {
                Map.Entry entry = (Map.Entry) iter.next();
                Object key = entry.getKey();
                Object val = entry.getValue();
                sb.append(",");
                sb.append(key);
                sb.append(":");
                sb.append(val);
            }
            this.canChoicePath = sb.toString().replaceFirst(",", "");
        }
    }

    @Override
    public String toString() {
        return "ActDefModel{" +
                "id=" + id +
                ", typeId=" + typeId +
                ", name='" + name + '\'' +
                ", modelKey='" + modelKey + '\'' +
                ", taskNameRule='" + taskNameRule + '\'' +
                ", description='" + description + '\'' +
                ", status=" + status +
                ", modelEditorJson='" + modelEditorJson + '\'' +
                ", deployId=" + deployId +
                ", procDefKey='" + procDefKey + '\'' +
                ", procDefId='" + procDefId + '\'' +
                ", modelComment='" + modelComment + '\'' +
                ", version=" + version +
                ", typeName='" + typeName + '\'' +
                ", toFirstNode=" + toFirstNode +
                ", showFirstAssignee=" + showFirstAssignee +
                ", canChoicePath='" + canChoicePath + '\'' +
                ", canChoicePathNodeMap=" + canChoicePathNodeMap +
                ", isUseOutForm=" + isUseOutForm +
                ", formDetailUrl='" + formDetailUrl + '\'' +
                ", allowFinishedCc=" + allowFinishedCc +
                ", submitConfirm=" + submitConfirm +
                ", allowDivert=" + allowDivert +
                ", informStart='" + informStart + '\'' +
                ", informType='" + informType + '\'' +
                ", attachment=" + attachment +
                ", sameExecutorJump=" + sameExecutorJump +
                ", allowRefer=" + allowRefer +
                ", instanceAmount=" + instanceAmount +
                ", allowFinishedDivert=" + allowFinishedDivert +
                ", isPrintForm=" + isPrintForm +
                ", directstart=" + directstart +
                ", ccMessageType='" + ccMessageType + '\'' +
                ", testStatusTag='" + testStatusTag + '\'' +
                ", allowMobile=" + allowMobile +
                ", allowRevert=" + allowRevert +
                ", skipSetting='" + skipSetting + '\'' +
                ", thumbnail=" + Arrays.toString(thumbnail) +
                ", createBy='" + createBy + '\'' +
                ", createTime=" + createTime +
                ", updateTime=" + updateTime +
                ", updateBy='" + updateBy + '\'' +
                ", tenantId='" + tenantId + '\'' +
                '}';
    }
}
